package com.hoshiicloud.mallshop.service.impl.goodsmanage;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.query.OmnipotentParam;
import com.hoshiicloud.common.query.OmnipotentWrapper;
import com.hoshiicloud.goods.entity.goods.Gallery;
import com.hoshiicloud.goods.entity.goods.GoodsGallery;
import com.hoshiicloud.goods.entity.sku.*;
import com.hoshiicloud.goods.entity.spu.*;
import com.hoshiicloud.goods.param.sku.*;
import com.hoshiicloud.goods.param.spu.BaseSpuUpdateParam;
import com.hoshiicloud.goods.param.spu.ExtSpuUpdateParam;
import com.hoshiicloud.goods.vo.ExtSpuDisplayVo;
import com.hoshiicloud.mallshop.feign.ShopGoods2FeignService;
import com.hoshiicloud.mallshop.feign.ShopGoodsSpuSkuFeignService;
import com.hoshiicloud.mallshop.feign.ShopIdGeneratorService;
import com.hoshiicloud.mallshop.feign.ShopUserFeignService;
import com.hoshiicloud.mallshop.mapper.product.ProductManageMapper;
import com.hoshiicloud.mallshop.param.ProductSkuParam;
import com.hoshiicloud.mallshop.param.ProductSpuParam;
import com.hoshiicloud.mallshop.service.goodsmanage.MainStoreGoodsManageService;
import com.hoshiicloud.mallshop.vo.ProductSkuVo;
import com.hoshiicloud.mallshop.vo.ProductSpuVo;
import com.hoshiicloud.user.entity.store.PlatStore;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品管理
 * linjx on 2019/9/17
 */
@Service
@Slf4j
public class MainStoreGoodsManageServiceImpl implements MainStoreGoodsManageService {
    private final ShopIdGeneratorService idGeneratorService;
    private final ProductManageMapper productManageMapper;
    private final ShopGoodsSpuSkuFeignService spuSkuFeignService;
    private final ShopUserFeignService userFeignService;
    private final ShopGoods2FeignService goods2FeignService;

    public MainStoreGoodsManageServiceImpl(ShopIdGeneratorService idGeneratorService, ProductManageMapper productManageMapper, ShopGoodsSpuSkuFeignService spuSkuFeignService, ShopUserFeignService userFeignService, ShopGoods2FeignService goods2FeignService) {
        this.idGeneratorService = idGeneratorService;
        this.productManageMapper = productManageMapper;
        this.spuSkuFeignService = spuSkuFeignService;
        this.userFeignService = userFeignService;
        this.goods2FeignService = goods2FeignService;
    }

    /**
     * 分页查询商品列表
     */
    @Override
    public Page listProduct(Map<String, Object> map) {
        Page page = new Page(Long.valueOf(map.get("pageNumber").toString()), Long.valueOf(map.get("pageSize").toString()));

        String orderField = map.getOrDefault("orderField", "createdTime").toString();
        String orderType = map.getOrDefault("orderType", "DESC").toString();
        if (StringUtils.isBlank(orderField)) {
            orderField = "createdTime";
        }
        if (StringUtils.isBlank(orderType)) {
            orderType = "DESC";
        }
        map.put("orderByCondition", "ORDER BY " + orderField + " " + orderType);

        List<ExtSpuDisplayVo> extSpuDisplayVos = productManageMapper.listProduct(page, map);
        // 数据处理：图片改为数组的形式， 通过类目Id查询类目的中文名称
        this.dateHandle(extSpuDisplayVos);
        page.setRecords(extSpuDisplayVos);
        return page;
    }

    /**
     * 商品上下架  status: 1-上架商品  2-下架商品
     */
    @Override
    public Boolean productShelvesUpAndDown(Map<String, Object> map, Integer status) {

        Long extSpuId = Long.parseLong(map.get("extId") + "");
        Long baseSpuId = spuSkuFeignService.getExtSpuById(extSpuId).getData().getSpuId();
        List<Long> storeIds = this.listStoreIds(Long.parseLong(map.get("storeId") + ""));
        // 上下架 ExtSpu
        this.extSpuUpAndDown(map, baseSpuId, storeIds, status);
        // 上下架 ExtSku
        this.extSkuUpAndDown(map, baseSpuId, storeIds, status);
        return true;
    }

    /**
     * 批量删除商品
     */
    @Override
    public Boolean deleteProductBatch(Map<String, Object> map) {

        // 更新人
        String updateBy = map.get("updateBy") + "";

        // 总店以及分店Id
        List<Long> storeIds = this.listStoreIds(Long.parseLong(map.get("storeId").toString()));

        // 获取总店的 extSpu信息
        List<Long> listExtSpuByIds = this.listDeleteExtSkuId(map);
        List<ExtSpu> extSpus = spuSkuFeignService.listExtSpuByIds(listExtSpuByIds).getData();
        Set<Long> baseSpuIds = extSpus.stream().map(ExtSpu::getSpuId).collect(Collectors.toSet());

        // 删除BaseSpu
        this.deleteBaseSpu(baseSpuIds, updateBy);

        // 删除 baseGoodsGallery
        this.deletedBaseGoodsGallery(baseSpuIds);

        // 删除ExtSpu
        this.deleteExtSpu(storeIds, baseSpuIds, updateBy);

        // 删除sku的相关信息
        this.deleteSku(storeIds, baseSpuIds, map, updateBy);

        return true;
    }

    /**
     * 获取需要编辑的商品信息
     */
    @Override
    public ProductSpuVo getNeedEditProduct(Map<String, Object> map) {

        // 查询spu相关信息
        ProductSpuVo productSpuVo = productManageMapper.getNeedEditProduct(map);
        if (productSpuVo == null) {
            throw new ServiceException("编辑失败，暂无相关商品条目信息");
        }
        // 处理返回给前端的数据
        this.dataMessage(productSpuVo);
        // 查询sku相关信息
        map.put("extSpuId", productSpuVo.getExtSpuId());
        List<ProductSkuVo> skuList = productManageMapper.skuList(map);
        productSpuVo.setSkuList(skuList);
        return productSpuVo;
    }

    /**
     * 商品编辑总店
     */
    @Override
    public Boolean editProductForHeadStore(ProductSpuParam spuParam) {

        // 更新baseSpu
        BaseSpuUpdateParam baseSpuUpdateParam = this.setBaseSpuUpdateParam(spuParam);
        spuSkuFeignService.updateBaseSpu(baseSpuUpdateParam);

        // 更新ExtSpu
        ExtSpuUpdateParam extSpuUpdateParam = this.setExtSpuUpdateParam(spuParam);
        spuSkuFeignService.extSpuUpdate(extSpuUpdateParam);

        // 更新营销类目
        this.editSpuSalesCategory(spuParam);

        // 更新商品标签
        this.editExtSpuTags(spuParam);

        // 更新图片信息
        this.editGallery(spuParam);

        // 更新sku信息
        this.editSku(spuParam);
        return true;
    }

    /**
     * 商品编辑: 总店+分店
     */
    @Override
    public Boolean editAllProduct(ProductSpuParam spuParam) {

        // 获取需要更新的所有店铺的Id
        List<Long> storeIds = this.listStoreIds(spuParam.getStoreId());

        // 更新baseSpu
        BaseSpuUpdateParam baseSpuUpdateParam = this.setBaseSpuUpdateParam(spuParam);
        spuSkuFeignService.updateBaseSpu(baseSpuUpdateParam);

        // 更新ExtSpu
        this.editAllExtSpu(spuParam, storeIds);

        // 更新sku相关信息
        this.editAllSku(spuParam, storeIds);
        return null;
    }


    //--------------------------------------数据处理------------------------------------------

    private void dateHandle(List<ExtSpuDisplayVo> extSpuDisplayVos) {
        extSpuDisplayVos.forEach(baseSpuDisplayVo -> {
            String[] split = baseSpuDisplayVo.getSpuImage().split(",");
            List<String> images = Arrays.asList(split);
            baseSpuDisplayVo.setImageArr(images);
            baseSpuDisplayVo.setCategoryName(spuSkuFeignService.getGoodsCategoryById(Long.parseLong(baseSpuDisplayVo.getCategoryId() + "")).getData().getName());
        });
    }

    private List<Long> listStoreIds(Long parentStoreId) {
        List<PlatStore> stores = userFeignService.listBranchStore(parentStoreId).getData();
        List<Long> storeIds = stores.stream().map(PlatStore::getId).collect(Collectors.toList());
        storeIds.add(parentStoreId);
        return storeIds;
    }

    private void dataMessage(ProductSpuVo spuVo) {
        // 处理 类目相关 放回类目 id 集合， 以及类目名称集合
        spuVo.setCategoryIdStr(spuVo.getCategoryContext());

        // 图片出处理，分割图片传给前端
        List<String> images = Arrays.asList(spuVo.getDefaultImage().split(","));
        spuVo.setImageArr(images);

        spuVo.setCompleteHImage(spuVo.getHeImage());
        spuVo.setCompleteVImage(spuVo.getVeImage());
        spuVo.setCompleteSpuVideo(spuVo.getSpuVideo());

        // 查询商品标签信息
        List<ExtSpuTags> extSpuTags = goods2FeignService.getExtSpuTagsBySpuId(spuVo.getExtSpuId()).getData();
        List<String> tags = extSpuTags.stream().map(ExtSpuTags::getTagName).collect(Collectors.toList());
        spuVo.setExtSpuTags(tags);
    }

    //--------------------------------------商品上下架------------------------------------------

    private void extSpuUpAndDown(Map<String, Object> map, Long baseSpuId, List<Long> storeIds, Integer status) {

        OmnipotentParam<ExtSpu> param = new OmnipotentParam<>();
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "spu_id", baseSpuId));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "company_id", map.get("companyId")));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "platform_id", map.get("platformId")));
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "store_id", storeIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        ExtSpu extSpu = new ExtSpu();
        extSpu.setStatus(status);
        extSpu.setUpdatedBy(map.get("updateBy") + "");
        param.setWrappers(wrapperList);
        param.setEntity(extSpu);

        spuSkuFeignService.updateExtSpuBatch(param);
    }

    private void extSkuUpAndDown(Map<String, Object> map, Long baseSpuId, List<Long> storeIds, Integer status) {

        // 查询baseSku的Id信息
        List<OmnipotentWrapper> baseSkuWrapper = new ArrayList<>();
        baseSkuWrapper.add(OmnipotentWrapper.setWrapper("eq", "spu_id", baseSpuId));
        baseSkuWrapper.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        List<Long> baseSkuIds = spuSkuFeignService.listBaseSku(baseSkuWrapper).getData().stream().map(BaseSku::getId).collect(Collectors.toList());


        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "sku_id", baseSkuIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "company_id", map.get("companyId")));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "platform_id", map.get("platformId")));
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "store_id", storeIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));


        ExtSku extSkuUpdate = new ExtSku();
        extSkuUpdate.setStatus(status == 1 ? 2 : 3);
        extSkuUpdate.setUpdatedBy(map.get("updateBy") + "");

        OmnipotentParam<ExtSku> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(extSkuUpdate);

        spuSkuFeignService.updateExtSkuBatch(param);
    }

    //--------------------------------------批量删除------------------------------------------

    private List<Long> listDeleteExtSkuId(Map<String, Object> map) {
        String extSpuIdsStr = map.get("extSpuIds").toString().replace("[", "").replace("]", "");
        List<Long> extSpuIds = Arrays.asList(extSpuIdsStr.split(",")).stream().map(s -> Long.valueOf(s.trim())).collect(Collectors.toList());
        return extSpuIds;
    }

    private void deleteBaseSpu(Set<Long> baseSpuIds, String updateBy) {

        OmnipotentParam<BaseSpu> param = new OmnipotentParam<>();

        List<OmnipotentWrapper> wrappers = new ArrayList<>();
        wrappers.add(OmnipotentWrapper.setWrapper("in", "id", baseSpuIds));
        wrappers.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        BaseSpu baseSpu = new BaseSpu();
        baseSpu.setDeleted(1);
        baseSpu.setUpdatedBy(updateBy);
        baseSpu.setUpdatedTime(LocalDateTime.now());

        param.setWrappers(wrappers);
        param.setEntity(baseSpu);

        spuSkuFeignService.updateBaseSpuBatch(param);
    }

    private void deletedBaseGoodsGallery(Set<Long> baseSpuIds) {

        OmnipotentParam<BaseGoodsGallery> omni = new OmnipotentParam<>();
        List<OmnipotentWrapper> wrappers = new ArrayList<>();
        wrappers.add(OmnipotentWrapper.setWrapper("in", "goods_id", baseSpuIds));
        wrappers.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        BaseGoodsGallery entity = new BaseGoodsGallery();
        entity.setDeleted(1);
        entity.setUpdateTime(LocalDateTime.now());

        omni.setWrappers(wrappers);
        omni.setEntity(entity);
        spuSkuFeignService.updateBaseGoodsGalleryBatch(omni);

    }

    private void deleteExtSpu(List<Long> storeIds, Set<Long> baseSpuIds, String updateBy) {

        // 通过店铺Id 和 baseSpuId 查询总店和分店的 ExtSpuId 集合
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "store_id", storeIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "spu_id", baseSpuIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        List<ExtSpu> extSpus = spuSkuFeignService.queryOmnipotentExtSpu(wrapperList).getData();
        List<Long> extSpuIds = extSpus.stream().map(ExtSpu::getId).collect(Collectors.toList());

        OmnipotentParam<ExtSpu> param = new OmnipotentParam<>();
        List<OmnipotentWrapper> wrappers = new ArrayList<>();
        wrappers.add(OmnipotentWrapper.setWrapper("in", "id", extSpuIds));

        ExtSpu extSpu = new ExtSpu();
        extSpu.setDeleted(1);
        extSpu.setStatus(2);
        extSpu.setUpdatedBy(updateBy);
        extSpu.setUpdatedTime(LocalDateTime.now());

        param.setWrappers(wrappers);
        param.setEntity(extSpu);
        spuSkuFeignService.updateExtSpuBatch(param);

        // 删除与ExtSpu相关联表的信息
        this.deletedExtSpuRelation(extSpuIds, updateBy);
    }

    private void deletedExtSpuRelation(List<Long> extSpuIds, String updateBy) {
        // 删除营销类目
        this.deleteSpuSalesCategory(extSpuIds, updateBy);
        // 删除商品标签信息
        this.deleteExtSpuTags(extSpuIds, updateBy);
        // 删除商品图片资源表以及相册的信息
        this.deleteGoodsGalleryAndGallery(extSpuIds);
    }

    private void deleteSpuSalesCategory(List<Long> extSpuIds, String updateBy) {
        List<OmnipotentWrapper> spuSalesWrappers = new ArrayList<>();
        spuSalesWrappers.add(OmnipotentWrapper.setWrapper("in", "ext_spu_id", extSpuIds));
        spuSalesWrappers.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        SpuSalesCategory spuSalesCategory = new SpuSalesCategory();
        spuSalesCategory.setDeleted(1);
        spuSalesCategory.setUpdatedBy(updateBy);
        spuSalesCategory.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<SpuSalesCategory> spuSalesParam = new OmnipotentParam<>();
        spuSalesParam.setWrappers(spuSalesWrappers);
        spuSalesParam.setEntity(spuSalesCategory);
        goods2FeignService.updateSpuSalesCategoryBatch(spuSalesParam);
    }

    private void deleteExtSpuTags(List<Long> extSpuIds, String updateBy) {
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "spu_id", extSpuIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        ExtSpuTags entity = new ExtSpuTags();
        entity.setDeleted(1);
        entity.setUpdatedBy(updateBy);
        entity.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<ExtSpuTags> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(entity);
        goods2FeignService.updateExtSpuTagsBatch(param);
    }

    private void deleteGoodsGalleryAndGallery(List<Long> extSpuIds) {

        // 通过extSpuId 查询商品图片资源信息
        List<GoodsGallery> goodsGalleries = goods2FeignService.listGoodsGalleryBySpuIds(extSpuIds).getData();

        // 删除相册信息
        List<Long> galleryList = goodsGalleries.stream().map(GoodsGallery::getGalleryId).collect(Collectors.toList());

        List<OmnipotentWrapper> galleryWrapperList = new ArrayList<>();
        galleryWrapperList.add(OmnipotentWrapper.setWrapper("in", "id", galleryList));

        Gallery gallery = new Gallery();
        gallery.setDeleted(1);
        gallery.setUpdateTime(LocalDateTime.now());

        OmnipotentParam<Gallery> galleryParam = new OmnipotentParam<>();
        galleryParam.setWrappers(galleryWrapperList);
        galleryParam.setEntity(gallery);
        spuSkuFeignService.updateGalleryBatch(galleryParam);

        // 删除商品图片资源表信息
        List<Long> goodsGalleryIds = goodsGalleries.stream().map(GoodsGallery::getId).collect(Collectors.toList());

        List<OmnipotentWrapper> goodsGalleryWrapperList = new ArrayList<>();
        goodsGalleryWrapperList.add(OmnipotentWrapper.setWrapper("in", "id", goodsGalleryIds));

        GoodsGallery goodsGallery = new GoodsGallery();
        goodsGallery.setDeleted(1);

        OmnipotentParam<GoodsGallery> goodsGalleyParam = new OmnipotentParam<>();
        goodsGalleyParam.setWrappers(goodsGalleryWrapperList);
        goodsGalleyParam.setEntity(goodsGallery);
        goods2FeignService.updateGoodsGalleyBatch(goodsGalleyParam);

    }

    private void deleteSku(List<Long> storeIds, Set<Long> baseSpuIds, Map<String, Object> map, String updateBy) {

        // 获取需要删除的 BaseSkuId 的集合
        List<OmnipotentWrapper> baseSkuWrapperList = new ArrayList<>();
        baseSkuWrapperList.add(OmnipotentWrapper.setWrapper("in", "spu_id", baseSpuIds));
        baseSkuWrapperList.add(OmnipotentWrapper.setWrapper("eq", "company_id", map.get("companyId")));
        baseSkuWrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        List<Long> baseSkuIds = spuSkuFeignService.listBaseSku(baseSkuWrapperList).getData().stream().map(BaseSku::getId).collect(Collectors.toList());

        // 获取需要删除的 ExtSkuId 的集合
        List<OmnipotentWrapper> extSkuWrapperList = new ArrayList<>();
        extSkuWrapperList.add(OmnipotentWrapper.setWrapper("in", "sku_id", baseSkuIds));
        extSkuWrapperList.add(OmnipotentWrapper.setWrapper("in", "store_id", storeIds));
        extSkuWrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        List<Long> extSkuIds = spuSkuFeignService.listExtSku(extSkuWrapperList).getData().stream().map(ExtSku::getId).collect(Collectors.toList());

        // 删除 BaseSku
        this.deleteBseSku(baseSkuIds, updateBy);

        // 删除 GoodsSkuGallery
        this.deleteBaseSkuGallery(baseSkuIds, updateBy);

        // 删除ExtSku
        this.deleteExtSku(extSkuIds, updateBy);

        // 删除productMange
        this.deleteProductMange(extSkuIds, updateBy);

        // 删除 vendorGoodsSku
        this.deleteVendorGoodsSku(baseSkuIds, updateBy);

    }

    private void deleteBseSku(List<Long> baseSkuIds, String updateBy) {

        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "id", baseSkuIds));

        BaseSku baseSku = new BaseSku();
        baseSku.setDeleted(1);
        baseSku.setUpdatedBy(updateBy);
        baseSku.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<BaseSku> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(baseSku);

        spuSkuFeignService.updateBaseSkuBatch(param);

    }

    private void deleteBaseSkuGallery(List<Long> baseSkuIds, String updateBy) {

        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "sku_id", baseSkuIds));

        BaseSkuGallery baseSkuGallery = new BaseSkuGallery();
        baseSkuGallery.setDeleted(1);
        baseSkuGallery.setUpdatedBy(updateBy);
        baseSkuGallery.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<BaseSkuGallery> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(baseSkuGallery);

        spuSkuFeignService.updateBaseSkuGalleryBatch(param);
    }

    private void deleteExtSku(List<Long> extSkuIds, String updateBy) {
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "id", extSkuIds));

        ExtSku extSku = new ExtSku();
        extSku.setDeleted(1);
        extSku.setStatus(3);
        extSku.setUpdatedBy(updateBy);
        extSku.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<ExtSku> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(extSku);

        spuSkuFeignService.updateExtSkuBatch(param);
    }

    private void deleteProductMange(List<Long> extSkuIds, String updateBy) {

        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "sku_id", extSkuIds));

        ProductManage productManage = new ProductManage();
        productManage.setDeleted(1);
        productManage.setUpdatedBy(updateBy);
        productManage.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<ProductManage> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(productManage);

        spuSkuFeignService.updateProductMange(param);
    }

    private void deleteVendorGoodsSku(List<Long> baseSkuId, String updateBy) {

        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "sku_id", baseSkuId));
        wrapperList.add(OmnipotentWrapper.setWrapper("isNull", "vendor_id", ""));

        VendorGoodsSku vendorGoodsSku = new VendorGoodsSku();
        vendorGoodsSku.setDeleted(1);
        vendorGoodsSku.setUpdatedBy(updateBy);
        vendorGoodsSku.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<VendorGoodsSku> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(vendorGoodsSku);

        spuSkuFeignService.updateVendorGoodsSkuBatch(param);
    }

    //--------------------------------------商品编辑: 总店------------------------------------------

    private void editSpuSalesCategory(ProductSpuParam spuParam) {

        List<OmnipotentWrapper> spuSalesWrappers = new ArrayList<>();
        spuSalesWrappers.add(OmnipotentWrapper.setWrapper("eq", "ext_spu_id", spuParam.getExtSpuId()));
        spuSalesWrappers.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        SpuSalesCategory spuSalesCategory = new SpuSalesCategory();
        // 类目Id
        spuSalesCategory.setSalesCategoryId(spuParam.getCategoryId());
        // BaseSpuId
        spuSalesCategory.setBaseSpuId(spuParam.getBaseSpuId());
        // ExtSpuId
        spuSalesCategory.setExtSpuId(spuParam.getExtSpuId());
        // 更新人
        spuSalesCategory.setUpdatedBy(spuParam.getUpdateBy());
        // 更新时间
        spuSalesCategory.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<SpuSalesCategory> spuSalesParam = new OmnipotentParam<>();
        spuSalesParam.setWrappers(spuSalesWrappers);
        spuSalesParam.setEntity(spuSalesCategory);
        goods2FeignService.updateSpuSalesCategoryBatch(spuSalesParam);
    }

    private void editExtSpuTags(ProductSpuParam spuParam) {
        // 1-通过extSpuId删除原有的标签信息
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "spu_id", spuParam.getExtSpuId()));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        goods2FeignService.removeExtSpuTagsBatch(wrapperList);

        // 2-新增标签重新与ExtSpu关联
        List<ExtSpuTags> extSpuTagsList = new ArrayList<>();
        List<String> extSpuTags = spuParam.getExtSpuTags();
        for (String tagName : extSpuTags) {

            ExtSpuTags extspuTag = new ExtSpuTags();
            // 生成Id
            Long tagId = this.getId();
            extspuTag.setId(tagId);
            // ExtSpuId
            extspuTag.setSpuId(spuParam.getExtSpuId());
            // BaseSpuId
            extspuTag.setBaseSpuId(spuParam.getBaseSpuId());
            // 标签名称
            extspuTag.setTagName(tagName);
            // 修改人
            extspuTag.setUpdatedBy(spuParam.getCreateBy());
            // 修改时间
            extspuTag.setUpdatedTime(LocalDateTime.now());
            extSpuTagsList.add(extspuTag);
        }
        goods2FeignService.saveExtSpuTagsBatch(extSpuTagsList);
    }

    private void editGallery(ProductSpuParam spuParam) {
        // 1-删除baseGoodsGallery 与 goodsGallery 的相关信息
        ArrayList<OmnipotentWrapper> baseGalleryWrapperList = new ArrayList<>();
        baseGalleryWrapperList.add(OmnipotentWrapper.setWrapper("eq", "goods_id", spuParam.getBaseSpuId()));
        spuSkuFeignService.removeBaseGoodsGalleryBatch(baseGalleryWrapperList);

        ArrayList<OmnipotentWrapper> goodsGalleryWrapperList = new ArrayList<>();
        goodsGalleryWrapperList.add(OmnipotentWrapper.setWrapper("eq", "ext_spu_id", spuParam.getExtSpuId()));
        goods2FeignService.removeGoodsGalleryBatch(goodsGalleryWrapperList);

        // 2- 新增 baseGoodsGallery、goodsGallery、gallery数据

        String imagesStr = spuParam.getDefaultImage();
        String[] images = imagesStr.split(",");

        // 保存相册信息
        List<Gallery> galleryList = new ArrayList<>();
        for (String image : images) {
            Long galleryId = this.getId();
            Gallery gallery = new Gallery();
            gallery.setId(galleryId);
            gallery.setImgUrl(image);
            gallery.setCreateTime(LocalDateTime.now());
            galleryList.add(gallery);
        }
        spuSkuFeignService.saveGalleryBath(galleryList);

        // 保存BaseSpu与 相册的关联信息
        List<BaseGoodsGallery> baseGoodsGalleryList = new ArrayList<>();
        for (Gallery gallery : galleryList) {
            Long baseGoodsGalleryId = this.getId();
            BaseGoodsGallery baseGoodsGallery = new BaseGoodsGallery();
            baseGoodsGallery.setId(baseGoodsGalleryId);
            baseGoodsGallery.setGoodsId(spuParam.getBaseSpuId());
            baseGoodsGallery.setGalleryId(gallery.getId());
            baseGoodsGallery.setCreateTime(gallery.getCreateTime());
            baseGoodsGalleryList.add(baseGoodsGallery);
        }
        spuSkuFeignService.saveBaseGoodsGalleryBatch(baseGoodsGalleryList);

        // 保存ExtSpu与相册的关联信息
        List<GoodsGallery> goodsGalleryList = new ArrayList<>();
        for (Gallery gallery : galleryList) {
            GoodsGallery goodsGallery = new GoodsGallery();
            Long goodsGalleryId = this.getId();
            goodsGallery.setId(goodsGalleryId);
            goodsGallery.setExtSpuId(spuParam.getExtSpuId());
            goodsGallery.setBaseSpuId(spuParam.getBaseSpuId());
            goodsGallery.setGalleryId(gallery.getId());
            goodsGalleryList.add(goodsGallery);
        }
        goods2FeignService.saveGoodsGalleryBatch(goodsGalleryList);
    }

    private void editSku(ProductSpuParam spuParam) {
        List<ProductSkuParam> skuList = spuParam.getSkuList();
        skuList.forEach(skuParam -> {

            // 更新BaseSku
            BaseSkuUpdateParam baseSkuUpdateParam = this.setBaseSkuUpdateParam(spuParam, skuParam);
            spuSkuFeignService.baseSkuUpdate(baseSkuUpdateParam);

            // 更新ExtSku
            ExtSkuUpdateParam extSkuUpdateParam = this.setExtSkuUpdateParam(spuParam, skuParam);
            spuSkuFeignService.extSkuUpdate(extSkuUpdateParam);

            // 更新VendorGoodsSku
            VendorGoodsSkuUpdateParam vendorGoodsSkuUpdateParam = this.setVendorGoodsSkuUpdateParam(spuParam, skuParam);
            spuSkuFeignService.vendorGoodsSkuUpdate(vendorGoodsSkuUpdateParam);

            // 更新ProductMange
            ProductManageUpdateParam productManageUpdateParam = this.setProductManageUpdateParam(spuParam, skuParam);
            spuSkuFeignService.updateProductMangerById(productManageUpdateParam);
        });
    }


    //--------------------------------------商品编辑: 总店+分店------------------------------------------

    private void editAllExtSpu(ProductSpuParam spuParam, List<Long> storeIds) {

        // 获取每个店铺的 extSpuId
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "store_id", storeIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "spu_id", spuParam.getBaseSpuId()));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        List<ExtSpu> extSpus = spuSkuFeignService.queryOmnipotentExtSpu(wrapperList).getData();
        List<Long> extSpuIds = extSpus.stream().map(ExtSpu::getId).collect(Collectors.toList());

        // 通过 extSpuIds 更新信息
        OmnipotentParam<ExtSpu> param = new OmnipotentParam<>();
        List<OmnipotentWrapper> wrappers = new ArrayList<>();
        wrappers.add(OmnipotentWrapper.setWrapper("in", "id", extSpuIds));

        ExtSpu extSpu = new ExtSpu();
        // spuId
        extSpu.setSpuId(spuParam.getBaseSpuId());
        // 商品标题
        extSpu.setTitle(spuParam.getTitle());
        // 存储商品主图
        extSpu.setSpuImage(spuParam.getDefaultImage().split(",")[0]);
        // 主图视频
        extSpu.setSpuVideo(spuParam.getSpuVideo());
        // 图文描述
        extSpu.setSpuDescription(spuParam.getSpuDescription());
        // 售后说明
        extSpu.setServiceDescription(spuParam.getServiceDescription());
        // 购买须知
        extSpu.setNotice(spuParam.getNotice());
        // 横图
        extSpu.setHImage(spuParam.getHeImage());
        // 竖图
        extSpu.setVImage(spuParam.getVeImage());
        // 权重
        extSpu.setWeight(spuParam.getWeight());
        // 更新人
        extSpu.setUpdatedBy(spuParam.getUpdateBy());

        param.setWrappers(wrappers);
        param.setEntity(extSpu);
        spuSkuFeignService.updateExtSpuBatch(param);

        this.editExtSpuRelation(spuParam, extSpuIds);
    }

    private void editExtSpuRelation(ProductSpuParam spuParam, List<Long> extSpuIds) {
        // 编辑营销类目
        this.editAllSpuSalesCategory(spuParam, extSpuIds);
        // 编辑商品标签
        this.editAllExtSpuTags(spuParam, extSpuIds);
        // 编辑相册等相关信息： gallery  base-goods-gallery  goods-gallery
        this.editAllGalleryRelation(spuParam, extSpuIds);
    }

    private void editAllSpuSalesCategory(ProductSpuParam spuParam, List<Long> extSpuIds) {

        List<OmnipotentWrapper> spuSalesWrappers = new ArrayList<>();
        spuSalesWrappers.add(OmnipotentWrapper.setWrapper("in", "ext_spu_id", extSpuIds));
        spuSalesWrappers.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));

        SpuSalesCategory spuSalesCategory = new SpuSalesCategory();
        // 类目Id
        spuSalesCategory.setSalesCategoryId(spuParam.getCategoryId());
        // BaseSpuId
        spuSalesCategory.setBaseSpuId(spuParam.getBaseSpuId());
        // ExtSpuId
        spuSalesCategory.setExtSpuId(spuParam.getExtSpuId());
        // 更新人
        spuSalesCategory.setUpdatedBy(spuParam.getUpdateBy());
        // 更新时间
        spuSalesCategory.setUpdatedTime(LocalDateTime.now());

        OmnipotentParam<SpuSalesCategory> spuSalesParam = new OmnipotentParam<>();
        spuSalesParam.setWrappers(spuSalesWrappers);
        spuSalesParam.setEntity(spuSalesCategory);
        goods2FeignService.updateSpuSalesCategoryBatch(spuSalesParam);
    }

    private void editAllExtSpuTags(ProductSpuParam spuParam, List<Long> extSpuIds) {

        // 1-通过extSpuId删除原有的标签信息
        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "spu_id", extSpuIds));
        wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
        goods2FeignService.removeExtSpuTagsBatch(wrapperList);

        // 2-新增标签重新与ExtSpu关联
        List<String> extSpuTags = spuParam.getExtSpuTags();

        for (Long extSpuId : extSpuIds) {
            List<ExtSpuTags> extSpuTagsList = new ArrayList<>();
            for (String tagName : extSpuTags) {
                ExtSpuTags extspuTag = new ExtSpuTags();
                // 生成Id
                Long tagId = this.getId();
                extspuTag.setId(tagId);
                // ExtSpuId
                extspuTag.setSpuId(extSpuId);
                // BaseSpuId
                extspuTag.setBaseSpuId(spuParam.getBaseSpuId());
                // 标签名称
                extspuTag.setTagName(tagName);
                // 修改人
                extspuTag.setUpdatedBy(spuParam.getCreateBy());
                // 修改时间
                extspuTag.setUpdatedTime(LocalDateTime.now());
                extSpuTagsList.add(extspuTag);
            }
            goods2FeignService.saveExtSpuTagsBatch(extSpuTagsList);
        }
    }

    private void editAllGalleryRelation(ProductSpuParam spuParam, List<Long> extSpuIds) {
        // 1-删除baseGoodsGallery 与 goodsGallery 的相关信息
        ArrayList<OmnipotentWrapper> baseGalleryWrapperList = new ArrayList<>();
        baseGalleryWrapperList.add(OmnipotentWrapper.setWrapper("eq", "goods_id", spuParam.getBaseSpuId()));
        spuSkuFeignService.removeBaseGoodsGalleryBatch(baseGalleryWrapperList);

        ArrayList<OmnipotentWrapper> goodsGalleryWrapperList = new ArrayList<>();
        goodsGalleryWrapperList.add(OmnipotentWrapper.setWrapper("in", "ext_spu_id", extSpuIds));
        goods2FeignService.removeGoodsGalleryBatch(goodsGalleryWrapperList);

        // 2- 新增 baseGoodsGallery、goodsGallery、gallery数据

        String imagesStr = spuParam.getDefaultImage();
        String[] images = imagesStr.split(",");

        // 保存相册信息
        List<Gallery> galleryList = new ArrayList<>();
        for (String image : images) {
            Long galleryId = this.getId();
            Gallery gallery = new Gallery();
            gallery.setId(galleryId);
            gallery.setImgUrl(image);
            gallery.setCreateTime(LocalDateTime.now());
            galleryList.add(gallery);
        }
        spuSkuFeignService.saveGalleryBath(galleryList);

        // 保存BaseSpu与 相册的关联信息
        List<BaseGoodsGallery> baseGoodsGalleryList = new ArrayList<>();
        for (Gallery gallery : galleryList) {
            Long baseGoodsGalleryId = this.getId();
            BaseGoodsGallery baseGoodsGallery = new BaseGoodsGallery();
            baseGoodsGallery.setId(baseGoodsGalleryId);
            baseGoodsGallery.setGoodsId(spuParam.getBaseSpuId());
            baseGoodsGallery.setGalleryId(gallery.getId());
            baseGoodsGallery.setCreateTime(gallery.getCreateTime());
            baseGoodsGalleryList.add(baseGoodsGallery);
        }
        spuSkuFeignService.saveBaseGoodsGalleryBatch(baseGoodsGalleryList);

        for (Long extSpuId : extSpuIds) {
            // 保存ExtSpu与相册的关联信息
            List<GoodsGallery> goodsGalleryList = new ArrayList<>();
            for (Gallery gallery : galleryList) {
                GoodsGallery goodsGallery = new GoodsGallery();
                Long goodsGalleryId = this.getId();
                goodsGallery.setId(goodsGalleryId);
                goodsGallery.setExtSpuId(extSpuId);
                goodsGallery.setBaseSpuId(spuParam.getBaseSpuId());
                goodsGallery.setGalleryId(gallery.getId());
                goodsGalleryList.add(goodsGallery);
            }
            goods2FeignService.saveGoodsGalleryBatch(goodsGalleryList);
        }
    }

    private void editAllSku(ProductSpuParam spuParam, List<Long> storeIds) {

        List<ProductSkuParam> skuList = spuParam.getSkuList();
        skuList.forEach(skuParam -> {
            // 更新BaseSku
            BaseSkuUpdateParam baseSkuUpdateParam = this.setBaseSkuUpdateParam(spuParam, skuParam);
            spuSkuFeignService.baseSkuUpdate(baseSkuUpdateParam);

            // 更新VendorGoodsSku
            VendorGoodsSkuUpdateParam vendorGoodsSkuUpdateParam = this.setVendorGoodsSkuUpdateParam(spuParam, skuParam);
            spuSkuFeignService.vendorGoodsSkuUpdate(vendorGoodsSkuUpdateParam);

            // 查询需要更新的ExtSku
            ArrayList<OmnipotentWrapper> wrapperList = new ArrayList<>();
            wrapperList.add(OmnipotentWrapper.setWrapper("eq", "sku_id", skuParam.getBaseSkuId()));
            wrapperList.add(OmnipotentWrapper.setWrapper("in", "store_id", storeIds));
            wrapperList.add(OmnipotentWrapper.setWrapper("eq", "deleted", 0));
            List<Long> extSkuIds = spuSkuFeignService.listExtSku(wrapperList).getData().stream().map(ExtSku::getId).collect(Collectors.toList());

            // 更新ExtSku
            this.editAllExtSku(spuParam, skuParam, extSkuIds);

            // 更新ProductMange
            this.editProductMange(spuParam, skuParam, extSkuIds);
        });
    }

    private void editAllExtSku(ProductSpuParam spuParam, ProductSkuParam skuParam, List<Long> extSkuIds) {

        Map<String, Object> map = new HashMap<>();
        map.put("spec_id", 3263976614391709706L);
        map.put("spec_name", "默认");
        map.put("spec_itemid", 3263976614391709716L);
        map.put("spec_itemname", "默认");
        Map<String, Object> map2 = new HashMap<>();
        map2.put("spec1", map);

        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "id", extSkuIds));

        ExtSku extSku = new ExtSku();
        // 标题
        extSku.setSkuTitle(spuParam.getTitle());
        // sku 图片
        extSku.setSkuImage(spuParam.getDefaultImage().split(",")[0]);
        // 售价
        extSku.setSalePrice(skuParam.getSalePrice());
        // 市场价
        extSku.setMarketPrice(skuParam.getMarketPrice());
        // 成本价
        extSku.setCostPrice(skuParam.getCostPrice());
        // 是否库库存足够才能下单  1 是 0 否
        extSku.setStoreEnough(1);
        // 规格
        extSku.setExtendSpec(JSONObject.toJSONString(map2));
        // 更新人
        extSku.setUpdatedBy(spuParam.getUpdateBy());
        // 收货间隔时间
        extSku.setReceiptInterval(skuParam.getReceiptInterval());
        // 提货时间
        extSku.setReceiptDate(skuParam.getReceiptDate());

        OmnipotentParam<ExtSku> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(extSku);
        spuSkuFeignService.updateExtSkuBatch(param);
    }

    private void editProductMange(ProductSpuParam spuParam, ProductSkuParam skuParam, List<Long> extSkuIds) {

        List<OmnipotentWrapper> wrapperList = new ArrayList<>();
        wrapperList.add(OmnipotentWrapper.setWrapper("in", "sku_id", extSkuIds));

        ProductManage productManage = new ProductManage();
        // 商品名称
        productManage.setSkuName(spuParam.getTitle());
        // 分配数量
        productManage.setQuantity(new BigDecimal(skuParam.getQuantity()));
        // 单位
        productManage.setUnit(skuParam.getPacketUnit());
        // 来源 1-租户  2-平台 3-店铺
        productManage.setSource(0);
        // 更新人
        productManage.setUpdatedBy(spuParam.getUpdateBy());
        // 冻结数量
        productManage.setFreezes(BigDecimal.ZERO);

        OmnipotentParam<ProductManage> param = new OmnipotentParam<>();
        param.setWrappers(wrapperList);
        param.setEntity(productManage);

        spuSkuFeignService.updateProductMange(param);
    }

    //--------------------------------------字段设置------------------------------------------

    private BaseSpuUpdateParam setBaseSpuUpdateParam(ProductSpuParam spuParam) {

        Map<String, Object> map = new HashMap<>();
        map.put("spec_id", 3263976614391709706L);
        map.put("spec_name", "默认");
        map.put("spec_itemid", 3263976614391709716L);
        map.put("spec_itemname", "默认");
        Map<String, Object> map2 = new HashMap<>();
        map2.put("spec1", map);

        return BaseSpuUpdateParam.builder()
                // id
                .id(spuParam.getBaseSpuId())
                // 名称
                .title(spuParam.getTitle())
                // 品牌
                .brandId(spuParam.getBrandId())
                // 商品类型
                .productType(spuParam.getProductType())
                // 采购类型
                .purchaseType(spuParam.getPurchaseType())
                // 属性
                .extendProperty(spuParam.getExtendProperty())
                // 默认图片
                .defaultImage(spuParam.getDefaultImage().split(",")[0])
                // 1-共有 2-私有
                .published(1)
                // 规格
                .extendSpec(JSONObject.toJSONString(map))
                // 更新人
                .updatedBy(spuParam.getUpdateBy())
                // 上下架状态  1-上架  2-下架
                .status(spuParam.getStatus())
                .build();
    }

    private ExtSpuUpdateParam setExtSpuUpdateParam(ProductSpuParam spuParam) {

        return ExtSpuUpdateParam.builder()
                // id
                .id(spuParam.getExtSpuId())
                // spuId
                .spuId(spuParam.getBaseSpuId())
                // 商品标题
                .title(spuParam.getTitle())
                // 存储商品主图
                .spuImage(spuParam.getDefaultImage().split(",")[0])
                // 主图视频
                .spuVideo(spuParam.getSpuVideo())
                // 图文描述
                .spuDescription(spuParam.getSpuDescription())
                // 售后说明
                .serviceDescription(spuParam.getServiceDescription())
                // 购买须知
                .notice(spuParam.getNotice())
                // 横图
                .hImage(spuParam.getHeImage())
                // 竖图
                .vImage(spuParam.getVeImage())
                // 更新人
                .updatedBy(spuParam.getUpdateBy())
                // 权重
                .weight(spuParam.getWeight()).build();
    }

    private BaseSkuUpdateParam setBaseSkuUpdateParam(ProductSpuParam spuParam, ProductSkuParam skuParam) {

        return BaseSkuUpdateParam.builder()
                .id(skuParam.getBaseSkuId())
                // spuid
                .spuId(spuParam.getBaseSpuId())
                // 商品标题
                .skuTitle(spuParam.getTitle())
                // 销售单位（*）
                .salesUnit(skuParam.getSalesUnit())
                // 条形码
                .barCode(skuParam.getBarCode())
                // 箱码
                .packetCode(skuParam.getPacketCode())
                // 规格
                .extendSpec(skuParam.getExtendSpec())
                // 是否负库存
                .isStored(0)
                // 更新人
                .updatedBy(spuParam.getUpdateBy())
                // 是否允许分销 0-不允许分销 1-允许分销
                .distributorStatu(skuParam.getDistributorStatu())
                // 是否允许分店编辑售价 0-不允许 1-允许
                .editStatu(skuParam.getEditStatu()).build();
    }

    private ExtSkuUpdateParam setExtSkuUpdateParam(ProductSpuParam spuParam, ProductSkuParam skuParam) {

        Map<String, Object> map = new HashMap<>();
        map.put("spec_id", 3263976614391709706L);
        map.put("spec_name", "默认");
        map.put("spec_itemid", 3263976614391709716L);
        map.put("spec_itemname", "默认");
        Map<String, Object> map2 = new HashMap<>();
        map2.put("spec1", map);

        return ExtSkuUpdateParam.builder()
                // id
                .id(skuParam.getExtSkuId())
                // 标题
                .skuTitle(spuParam.getTitle())
                // sku 图片
                .skuImage(spuParam.getDefaultImage().split(",")[0])
                // 售价
                .salePrice(skuParam.getSalePrice())
                // 市场价
                .marketPrice(skuParam.getMarketPrice())
                // 成本价
                .costPrice(skuParam.getCostPrice())
                // 是否库库存足够才能下单  1 是 0 否
                .storeEnough(1)
                // 规格
                .extendSpec(JSONObject.toJSONString(map2))
                // 更新人
                .updatedBy(spuParam.getUpdateBy())
                // 收货间隔时间
                .receiptInterval(skuParam.getReceiptInterval())
                // 提货时间
                .receiptDate(skuParam.getReceiptDate()).build();
    }

    private VendorGoodsSkuUpdateParam setVendorGoodsSkuUpdateParam(ProductSpuParam spuParam, ProductSkuParam skuParam) {
        return VendorGoodsSkuUpdateParam.builder()
                //id
                .id(skuParam.getVendorGoodsSkuId())
                // 条形码
                .barCode(skuParam.getBarCode())
                // 箱码
                .packetCode(skuParam.getPacketCode())
                // 包装单位
                .packetUnit(skuParam.getPacketUnit())
                // 内件数
                .innerBoxs(skuParam.getInnerBoxs())
                // 商品重量
                .weight(skuParam.getWeight())
                // 供价（不含税）
                .unitPrice(skuParam.getUnitprice())
                // 供价（含税）
                .taxUnitprice(skuParam.getTaxUnitprice())
                // 增值税率
                .taxRate(skuParam.getTaxRate())
                // 更新人
                .updatedBy(spuParam.getUpdateBy())
                // 采购类型
                .vendorPoType(skuParam.getVendorPoType()).build();
    }

    private ProductManageUpdateParam setProductManageUpdateParam(ProductSpuParam spuParam, ProductSkuParam skuParam) {
        return ProductManageUpdateParam.builder()
                //id
                .id(skuParam.getProductMangerId())
                // 商品名称
                .skuName(spuParam.getTitle())
                // 分配数量
                .quantity(new BigDecimal(skuParam.getQuantity()))
                // 单位
                .unit(skuParam.getPacketUnit())
                // 来源 1-租户  2-平台 3-店铺
                .source(0)
                // 更新人
                .updatedBy(spuParam.getUpdateBy())
                // 冻结数量
                .freezes(BigDecimal.ZERO).build();
    }

    /**
     * 生成一个Id 并且返回
     */
    private Long getId() {
        String id = idGeneratorService.queryGeneratorId();
        if (StrUtil.isBlank(id)) {
            throw new ServiceException(CommonError.error("主键ID生成失败"));
        }
        return Long.parseLong(id);
    }
}
